package com.hykj.coca.service;

import com.hykj.coca.mapper.UserMapper;
import com.hykj.coca.model.UserModel;
import com.hykj.coca.result.AppBack;
import com.hykj.coca.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserService extends BaseService<UserModel,UserMapper> {

    @Autowired
    private  UserMapper userMapper;

    @Autowired
    private  MeassgeService meassgeService;

    public UserModel nameCheck(HashMap map) {
        return userMapper.nameCheck(map);
    }

    public UserModel getByToken(UserModel userModel){
        return userMapper.getByToken(userModel);
    };

    public AppBack<Map> MobileLogin(String phone,String password){

        UserModel userModel=new UserModel();
        userModel.setPhone(phone);
        userModel.setPassword(password);

        UserModel userModel1Back=userMapper.login(userModel);
        if (userModel1Back == null ) {
            return new AppBack<>(-1, "用户名或密码错误");
        }
        if (userModel1Back.getStatus() <=0) {
            return new AppBack<>(-2, "您的账号不能正常使用");
        }
        String token = TokenUtil.getInstance().generateTokenUUID();
        userModel1Back.setToken(token);
        update(userModel1Back);
        Map map = new HashMap();
        map.put("userId", userModel1Back.getId());
        map.put("token", token);
        map.put("phone", userModel1Back.getPhone());
        if (userModel.getUsername()==null)
        map.put("userName","");
        else
            map.put("userName",userModel.getUsername());
        if (userModel.getLogo()==null)
        map.put("logo","");
        else
            map.put("logo",userModel.getLogo());
        map.put("userPwd",userModel.getPassword());

        return new AppBack<>(map);
    }

    @Override
    public UserModel change(UserModel model, Map condition) {
        if (condition.get("count")!=null){
            condition.put("user_id",model.getId());
           Integer count= meassgeService.getUnReadCount(condition);
            model.setCount(count);
        }

        return model;
    }
}
